<template>
  <div class="home-container">
    <!-- 顶部英雄区域 -->
    <div class="hero-section">
      <div class="hero-content">
        <h1>AI角色扮演平台</h1>
        <p>与来自不同领域的AI角色进行深度对话，学习、探索、创造</p>
        <div class="hero-actions">
          <button @click="exploreCharacters" class="btn-primary">浏览角色</button>
          <button @click="goToLogin" v-if="!isAuthenticated" class="btn-secondary">登录/注册</button>
        </div>
      </div>
    </div>

    <!-- 角色分类导航 -->
    <div class="categories-section">
      <div class="container">
        <h2>探索角色分类</h2>
        <div class="categories-list" v-if="categories.length > 0">
          <button
            v-for="category in categories"
            :key="category"
            @click="filterByCategory(category)"
            :class="['category-btn', { active: currentCategory === category }]"
          >
            {{ category }}
          </button>
        </div>
        <div v-else class="loading">加载中...</div>
      </div>
    </div>

    <!-- 角色列表 -->
    <div class="characters-section">
      <div class="container">
        <div class="section-header">
          <h2>热门角色</h2>
          <div class="search-bar">
            <input
              type="text"
              v-model="searchQuery"
              @input="searchCharacters"
              placeholder="搜索角色..."
              class="search-input"
            />
            <button @click="clearSearch" v-if="searchQuery" class="clear-btn">
              ×
            </button>
          </div>
        </div>

        <!-- 角色卡片列表 -->
        <div class="characters-grid" v-if="filteredCharacters.length > 0">
          <div v-for="character in filteredCharacters" :key="character.id" class="character-card">
            <div class="card-image">
              <img :src="character.avatar" :alt="character.name" class="character-avatar" />
            </div>
            <div class="card-content">
              <h3>{{ character.name }}</h3>
              <p class="character-description">{{ character.description }}</p>
              <div class="character-categories">
                <span v-for="category in character.categories" :key="category" class="category-tag">
                  {{ category }}
                </span>
              </div>
              <div class="card-actions">
                <router-link :to="{ name: 'character-detail', params: { id: character.id } }" class="btn-info">
                  了解更多
                </router-link>
                <router-link
                  :to="{ name: 'chat', params: { characterId: character.id } }"
                  class="btn-primary"
                  v-if="isAuthenticated"
                >
                  开始对话
                </router-link>
                <button @click="goToLogin" class="btn-secondary" v-else>
                  登录开始对话
                </button>
              </div>
            </div>
          </div>
        </div>

        <!-- 空状态 -->
        <div v-else-if="!characterStore.loading" class="empty-state">
          <p>没有找到符合条件的角色</p>
          <button @click="resetFilters" class="btn-secondary">重置筛选</button>
        </div>

        <!-- 加载状态 -->
        <div v-else class="loading">加载角色中...</div>

        <!-- 分页控件 -->
        <div class="pagination" v-if="characterStore.pagination?.totalPages > 1">
          <button
            @click="changePage(characterStore.pagination?.currentPage - 1)"
            :disabled="characterStore.pagination?.currentPage === 1"
            class="page-btn"
          >
            上一页
          </button>
          <span class="page-info">
            第 {{ characterStore.pagination?.currentPage }} / {{ characterStore.pagination?.totalPages }} 页
          </span>
          <button
            @click="changePage(characterStore.pagination?.currentPage + 1)"
            :disabled="characterStore.pagination?.currentPage === characterStore.pagination?.totalPages"
            class="page-btn"
          >
            下一页
          </button>
        </div>
      </div>
    </div>

    <!-- 功能介绍区域 -->
    <div class="features-section">
      <div class="container">
        <h2>平台特色</h2>
        <div class="features-grid">
          <div class="feature-card">
            <div class="feature-icon">
              <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
              </svg>
            </div>
            <h3>多样化角色</h3>
            <p>从历史人物到虚构角色，从科学家到艺术家，满足您的各种对话需求</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">
              <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"></path>
              </svg>
            </div>
            <h3>深度对话</h3>
            <p>基于AI技术的角色能够进行富有深度和连贯性的对话，提供沉浸式体验</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">
              <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"></path>
              </svg>
            </div>
            <h3>学习成长</h3>
            <p>通过与不同专业领域的AI角色对话，拓展知识边界，提升学习效率</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { characterStore } from '../store/character';
import { authStore } from '../store/auth';

const router = useRouter();
const characterStoreRef = characterStore();
const authStoreRef = authStore();

// 响应式数据
const searchQuery = ref('');
const currentCategory = ref('');

// 计算属性
const isAuthenticated = computed(() => authStoreRef.isAuthenticated);
const categories = computed(() => characterStoreRef.categories);
const filteredCharacters = computed(() => characterStoreRef.filteredCharacters);

// 方法
const exploreCharacters = () => {
  // 滚动到角色列表区域
  const charactersSection = document.querySelector('.characters-section');
  if (charactersSection) {
    charactersSection.scrollIntoView({ behavior: 'smooth' });
  }
};

const goToLogin = () => {
  router.push({ name: 'login' });
};

const searchCharacters = () => {
  characterStoreRef.setSearchQuery(searchQuery.value);
};

const clearSearch = () => {
  searchQuery.value = '';
  characterStoreRef.setSearchQuery('');
};

const filterByCategory = (category) => {
  currentCategory.value = currentCategory.value === category ? '' : category;
  characterStoreRef.setCurrentCategory(currentCategory.value);
};

const changePage = (page) => {
  if (page >= 1 && page <= (characterStoreRef.pagination?.totalPages || 1)) {
    characterStoreRef.getCharacters(page);
  }
};

const resetFilters = () => {
  searchQuery.value = '';
  currentCategory.value = '';
  characterStoreRef.setSearchQuery('');
  characterStoreRef.setCurrentCategory('');
};

// 生命周期钩子
onMounted(() => {
  // 加载角色列表和分类
  characterStoreRef.getCharacters();
  characterStoreRef.getCharacterCategories();
  // 加载用户认证状态
  authStoreRef.loadFromStorage();
});
</script>

<style scoped>
/* 全局样式 */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 英雄区域 */
.hero-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 100px 0;
  text-align: center;
}

.hero-content h1 {
  font-size: 3.5rem;
  margin-bottom: 20px;
  font-weight: 700;
}

.hero-content p {
  font-size: 1.2rem;
  margin-bottom: 30px;
  max-width: 600px;
  margin-left: auto;
  margin-right: auto;
}

.hero-actions {
  display: flex;
  justify-content: center;
  gap: 20px;
}

/* 分类区域 */
.categories-section {
  padding: 60px 0;
  background-color: #f8f9fa;
}

.categories-section h2 {
  text-align: center;
  margin-bottom: 30px;
  font-size: 2rem;
  color: #333;
}

.categories-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 15px;
}

.category-btn {
  padding: 10px 20px;
  border: 2px solid #667eea;
  background-color: white;
  color: #667eea;
  border-radius: 30px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 1rem;
  font-weight: 500;
}

.category-btn:hover {
  background-color: #667eea;
  color: white;
}

.category-btn.active {
  background-color: #667eea;
  color: white;
}

/* 角色列表区域 */
.characters-section {
  padding: 80px 0;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
  flex-wrap: wrap;
  gap: 20px;
}

.section-header h2 {
  font-size: 2rem;
  color: #333;
  margin: 0;
}

.search-bar {
  position: relative;
  max-width: 400px;
  width: 100%;
}

.search-input {
  width: 100%;
  padding: 12px 20px;
  border: 2px solid #e0e0e0;
  border-radius: 30px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.search-input:focus {
  outline: none;
  border-color: #667eea;
}

.clear-btn {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #667eea;
  cursor: pointer;
  padding: 5px;
}

.characters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}

.character-card {
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  background-color: white;
}

.character-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
}

.card-image {
  height: 200px;
  overflow: hidden;
  background-color: #f8f9fa;
}

.character-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.character-card:hover .character-avatar {
  transform: scale(1.05);
}

.card-content {
  padding: 20px;
}

.card-content h3 {
  margin-top: 0;
  margin-bottom: 10px;
  font-size: 1.5rem;
  color: #333;
}

.character-description {
  margin-bottom: 15px;
  color: #666;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.character-categories {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.category-tag {
  background-color: #f0f0f0;
  color: #666;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
}

.card-actions {
  display: flex;
  gap: 10px;
}

/* 功能介绍区域 */
.features-section {
  padding: 80px 0;
  background-color: #f8f9fa;
}

.features-section h2 {
  text-align: center;
  margin-bottom: 60px;
  font-size: 2rem;
  color: #333;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 40px;
}

.feature-card {
  text-align: center;
  padding: 40px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-icon {
  margin-bottom: 20px;
  color: #667eea;
}

.feature-card h3 {
  font-size: 1.5rem;
  margin-bottom: 15px;
  color: #333;
}

.feature-card p {
  color: #666;
  line-height: 1.6;
}

/* 分页控件 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
  margin-top: 40px;
}

.page-btn {
  padding: 10px 20px;
  border: 1px solid #667eea;
  background-color: white;
  color: #667eea;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.page-btn:hover:not(:disabled) {
  background-color: #667eea;
  color: white;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #666;
  font-size: 1rem;
}

/* 空状态和加载状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #666;
}

.empty-state p {
  margin-bottom: 20px;
  font-size: 1.1rem;
}

.loading {
  text-align: center;
  padding: 60px 20px;
  color: #666;
  font-size: 1.1rem;
}

/* 按钮样式 */
.btn-primary,
.btn-secondary,
.btn-info {
  padding: 12px 24px;
  border: none;
  border-radius: 5px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
  text-align: center;
  display: inline-block;
}

.btn-primary {
  background-color: #667eea;
  color: white;
}

.btn-primary:hover {
  background-color: #5a67d8;
}

.btn-secondary {
  background-color: #e2e8f0;
  color: #4a5568;
}

.btn-secondary:hover {
  background-color: #cbd5e0;
}

.btn-info {
  background-color: #3182ce;
  color: white;
}

.btn-info:hover {
  background-color: #2c5aa0;
}

/* 容器样式 */
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 大屏幕布局优化 */
@media (min-width: 1200px) {
  .characters-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  
  .features-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .hero-content h1 {
    font-size: 4rem;
  }
  
  .hero-content p {
    font-size: 1.3rem;
    max-width: 800px;
  }
}

/* 中等屏幕布局 */
@media (min-width: 769px) and (max-width: 1199px) {
  .characters-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .features-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* 小屏幕响应式设计 */
@media (max-width: 768px) {
  .hero-content h1 {
    font-size: 2.5rem;
  }

  .hero-actions {
    flex-direction: column;
    align-items: center;
  }

  .section-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .characters-grid {
    grid-template-columns: 1fr;
  }

  .features-grid {
    grid-template-columns: 1fr;
  }
}
</style>